<template>
    <div>   
        <table class="table">
            <tbody>
                <tr>
                    <td>用户姓名</td>
                    <td>
                        <input type="text" v-model="date.memberName">
                    </td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" value="1" name="sex" v-model="date.sex">男
                        <input type="radio" value="2" name="sex" v-model="date.sex" checked>女
                    </td>
                </tr>
                <tr>
                    <td>手机号</td>
                    <td>
                        <input type="text" v-model="date.phone">
                    </td>
                </tr>
                <tr>
                    <td>会员等级</td>
                    <td>
                        <select v-model="date.memberLevelId">
                            <option value="0">请选择</option>
                            <option v-for="a in data" :value="a.memberLevelId">{{ a.memberLevelName }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>消费金额</td>
                    <td>
                        <input type="text" v-model="date.price">
                    </td>
                </tr>
                <tr>
                    <td>消费次数</td>
                    <td>
                        <input type="text" v-model="date.num">
                    </td>
                </tr>
                <tr>
                    <td>可用积分</td>
                    <td>
                        <input type="text" v-model="date.intation">
                    </td>
                </tr>
                <tr>
                    <td>可用余额</td>
                    <td>
                        <input type="text" v-model="date.kPrice">
                    </td>
                </tr>
                <tr>
                    <td>启用状态</td>
                    <td>
                        <input type="checkbox" v-model="date.state">勾选为"开启",否则"关闭"
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="2">
                        <input type="button" value="确定" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
const routher = useRouter()
onMounted(()=>{
    getdate()
})
const getdate=()=>{
    axios.get("https://localhost:7126/api/MemberLevel/GetMemberLevels").then(res=>{
        data.value=res.data
    })
}
const data=ref([{
    "memberLevelId": 0,
    "memberLevelName": ""
}])

const add=()=>{
    var p=/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    if(!p.test(date.value.phone)){
        alert("手机号格式不正确")
        return
    }
    if(date.value.memberName==""){
        alert("姓名不能为空")
        return
    }
    if(date.value.phone==""){
        alert("手机号不能为空")
        return
    }
    if(date.value.price==""){
        alert("消费金额不能为空")
        return
    }
    if(date.value.num==""){
        alert("消费次数不能为空")
        return
    }
    if(date.value.intation==""){
        alert("可用积分不能为空")
        return
    }
    if(date.value.kPrice==""){
        alert("可用金额不能为空")
        return
    }
    if(date.value.intation<1||date.value.intation>1000){
        alert("可用积分不得低于0分，不得高于1000分")
        return
    }

    axios.post("https://localhost:7126/api/Member/AddMember",date.value).then(res=>{
        if(res.data>0){
            alert("新增成功")
            routher.push({path:"/Show"})
        }
        else{
            alert("姓名不能重复")
        }
    })
}
const date=ref({
  "memberId": 0,
  "memberName": "",
  "sex": 0,
  "phone": "",
  "memberLevelId": 0,
  "price": 0,
  "num": 0,
  "intation": 0,
  "kPrice": 0,
  "state": false,
  //"createDate": ""
})
</script>

<style scoped>

</style>